﻿@namespace Project.Web.Shared.Components
@typeparam TModel
@inherits BasicComponent
@{
    int index = 0;
}
@if (Condition.Children.Count == 0)
{
    @UI.BuildButton(this).Text(TableLocalizer["AdvanceCondition.AddCondition"]).Primary().OnClick(() => Condition.Children.Add(new())).Render()
}
<div>
    <CascadingValue Value="this" IsFixed="true">
        <CascadingValue Value="Localizer" Name="@nameof(Localizer)" IsFixed="true">
            <CascadingValue Value="TableLocalizer" Name="@nameof(TableLocalizer)" IsFixed="true">
                @foreach (var item in Condition.Children)
                {
                    <Group Item="item"
                           TModel="TModel"
                           Columns="Columns"
                           UI="@UI"
                           Index="index"
                           Indent="@Indent"
                           Parent="Condition"></Group>
                    index++;
                }
            </CascadingValue>
        </CascadingValue>
    </CascadingValue>
</div>


@code {
    [Parameter, NotNull] public ConditionUnit? Condition { get; set; }
    [Parameter, NotNull] public EventCallback<ConditionUnit> ConditionChanged { get; set; }
    [Parameter, NotNull] public IStringLocalizer? Localizer { get; set; }
    [Parameter, NotNull] public IStringLocalizer? TableLocalizer { get; set; }
    [Parameter] public int Indent { get; set; } = 20;
    private ColumnInfo[]? Columns;
    public SelectItem<LinkType> LinkOptions = new();
    protected override void OnInitialized()
    {
        LinkOptions.Add(TableLocalizer?["ConditionSwitch.AndAlso"] ?? "并且", LinkType.AndAlso);
        LinkOptions.Add(TableLocalizer?["ConditionSwitch.OrElse"] ?? "或者", LinkType.OrElse);
        base.OnInitialized();
        Columns = typeof(TModel).GenerateColumns();
        Condition ??= new();
        Condition.LinkType = LinkType.None;
    }

    public Task UpdateView() => InvokeAsync(StateHasChanged);

}
